package com.xdl.moudle.utils.base;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xdl.moudle.utils.util.NetWorkUtil;
import com.xdl.moudle.utils.util.apibase.APIBaseResult;
import com.xdl.moudle.utils.util.bean.AppAction;


/**
 * 增加公共参数
 */
public abstract class BaseController {

	protected final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private HttpServletRequest req;

	protected APIBaseResult getAPIResult(APIBaseResult result) {
		return result;
	}

	public void loggerInfo(String path, Object params) {
		log.info("\n request : {} \n params : {}" , path, params);
	}

	public void loggerInfo(APIBaseResult result, String path, Object params, long now) {
		log.info("\n request : {} \n params : {}" + "\n success :{} \n time : "+ (System.currentTimeMillis() - now) + "ms", path, params, result);
	}

	public void loggerError(String path, Object params, Exception e) {
		e.printStackTrace();
		log.info("\n request : {} \n params : {}" + "\n exception :{} ", path,  params,  e.getMessage());
	}

	/**
	 * 获取公共参数
	 * 
	 * @return
	 * @throws Exception
	 */
	protected AppAction getPublicParams() {
		AppAction appAction = new AppAction();
		try {
			String ipAddress = NetWorkUtil.getIpAddress(req); // 获取客户端IP地址
			String token = req.getParameter("Token");
			String version = req.getParameter("Version");
			String patform = req.getParameter("Patform");
			String dateKey = req.getParameter("DateKey");
			String iswifi = req.getParameter("Iswifi");
			String sign = req.getParameter("Sign");
			String currenPage = req.getParameter("CurrenPage");
			String pageSize = req.getParameter("PageSize");
			String requestURL = req.getRequestURI();
			
			appAction.setIpAddress(ipAddress);
			appAction.setToken(token);
			appAction.setVersion(version);
			appAction.setPatform(patform);
			appAction.setDateKey(dateKey);
			appAction.setIswifi(iswifi);
			appAction.setSign(sign);
			appAction.setRequestURL(requestURL);
			if (StringUtils.isNotBlank(currenPage)) {
				appAction.setCurrenPage(Integer.valueOf(currenPage));
			}
			if (StringUtils.isNotBlank(pageSize)) {
				appAction.setPageSize(Integer.valueOf(pageSize));
			}
		} catch (Exception e) {
			log.error("获取公共参数getPublicParams方法出错:" + e);
			e.printStackTrace();
		}
		return appAction;
	}

}
